package ca.etsmtl.log730.projet.controller.client;

import java.util.Map;
import java.util.UUID;

import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import ca.etsmtl.log730.projet.controller.AbstractSessionController;
import ca.etsmtl.log730.projet.controller.nmessages.Reply;
import ca.etsmtl.log730.projet.controller.nmessages.ReplyType;
import ca.etsmtl.log730.projet.controller.nmessages.commands.FileAction;
import ca.etsmtl.log730.projet.controller.nmessages.commands.FileCommand;
import ca.etsmtl.log730.projet.controller.nmessages.commands.ListCommand;
import ca.etsmtl.log730.projet.controller.nmessages.commands.ObjectType;
import ca.etsmtl.log730.projet.controller.nmessages.commands.SessionAction;
import ca.etsmtl.log730.projet.controller.nmessages.commands.SessionCommand;
import ca.etsmtl.log730.projet.model.user.User;
import ca.etsmtl.log730.projet.model.workspace.TextObject;

public class ClientControllerHandler extends AbstractSessionController implements ClientWorkspaceManager {
	public ClientControllerHandler() {
		this(null, null);
	}
	public ClientControllerHandler(IoSession session, UUID sessionId) {
		super(session, sessionId);
	}

	@Override
	public void exceptionCaught(IoSession ios, Throwable arg1)
			throws Exception {
	}

	@Override
	public void messageReceived(IoSession ios, Object o) throws Exception {
		if(o instanceof Reply) {
			Reply r = (Reply)o;
			if(setUser != null && setUserConfirmed == false && r.getOriginId() == setUser.ID) {
				if(r.getReply() == ReplyType.OK) setUserConfirmed = true;
			}
		}
	}

	@Override
	public void messageSent(IoSession ios, Object o) throws Exception {
		System.out.println("-> " + o.toString());
	}

	@Override
	public void sessionClosed(IoSession ios) throws Exception {
	}

	@Override
	public void sessionCreated(IoSession ios) throws Exception {
//		doListCommand(ios, ObjectType.FILE);
//		doListCommand(ios, ObjectType.USER);

	}

	@Override
	public void sessionIdle(IoSession ios, IdleStatus arg1) throws Exception {
//		setUser(new User("MAN"));
	}

	@Override
	public void sessionOpened(IoSession ios) throws Exception {
	}

	
	
	
	
	
	@Override
	public Map<UUID, TextObject> getObjects() {
		return null;
	}
	@Override
	public Map<UUID, User> getUsers() {
		return null;
	}
	
	private SessionCommand setUser = null;
	private boolean setUserConfirmed = false;
	
	@Override
	public void setUser(User u) {
		setUserConfirmed = false;
		setUser = new SessionCommand(SessionAction.IDENT, u);
//		writeObject(getSession(), setUser);
	}
	@Override
	public User getUser() {
		return (User)setUser.getParam();
	}
	
	
	@Override
	public void createFile(TextObject object) {
	}
	@Override
	public void deleteFile(UUID fileId) {
	}
	
	
	@Override
	public void requestLock(UUID fileId) {
	}
	@Override
	public void removeLock(UUID fileId) {
	}
}
